<?php
declare (strict_types=1);

namespace app\admin\controller;

use addons\BuilderPlus\components\Button;
use addons\BuilderPlus\components\Column as Col;
use addons\BuilderPlus\facade\Table;

class SummaryUser extends Base
{
    public function getOptions(): array
    {
        return [
            'pk'    => 'id',
            'model' => \app\common\model\SummaryUser::class,
            'with'  => ['user']
        ];
    }

    public function index(): string
    {
        $table = Table::init($this);
        $table->addColumns([
            ['code', '编号', fn(Col $col) => $col->fixed('left')->templet('{{d.user.code}}')->totalRowText('合计')],
            ['nickname', '昵称', fn(Col $col) => $col->fixed('left')->templet('{{d.user.nickname}}')],
            ['mobile', '手机号', fn(Col $col) => $col->fixed('left')->templet('{{d.user.mobile}}')]
        ]);
        foreach (getBonusTitle(2) as $k => $v) {
            $table->addColumn($k, $v, fn(Col $col) => $col->totalRow(true));
        }
        return $table
            ->setCheckbox(false)
            ->addColumn('update_time', '更新时间')
            ->addRightButtons([
                'edit' => fn(Button $b) => $b->title('查看明细')->url(url('summary_info')->build())
            ])
            ->addSearch([
                ['uid', '会员编号/昵称/手机号', fn($s) => $s->relation('\app\common\model\User', 'code|nickname|mobile,id')],
            ])
            ->setTotalRow(true)
            ->enableExport()
            ->fetch();
    }


    public function summary_info($id)
    {
        $info = \app\common\model\SummaryUser::find($id);
        $url = url('account/bonus') . '?uid=' . $info->user->code;
        return redirect($url);

    }

}
